<?php
header("content-type:text/html;charset=utf8");
$pdo = new PDO("mysql:host=127.0.0.1;charset=utf8;dbname=peng",'root','root');
$redis = new Redis();
$redis->connect('127.0.0.1','6379');
$page = isset($_GET['page'])?$_GET['page']:1;//获取当前页
$size = 3;                  //设置每页显示条数
$count = $pdo->query("select count(*) from user")->fetch();
$count = $count['count(*)'];       //查询总条数
$size = 3;//每页显示条数
$totalnum = ceil($count/$size);//总条数
$up = $page-1>0?$page-1:1;                //上一页
$next = $page+1>$totalnum?$totalnum:$page+1; //下一页
$offset = ($page-1)*$size;
if ($redis->get("user".$page)) {
	echo "这是redis查的数据";
	$data = $redis->get("user".$page);
	$data = json_decode($data,true);
	// print_r($data);die;
}
else
{
	$data = $pdo->query("select * from user limit $offset,$size")->fetchall(PDO::FETCH_ASSOC);
	$strdata = json_encode($data);
	$redis->set("user".$page,$strdata);

}

// $str = "<a href='show.php?page=1'>首页</a><a href='show.php?page=$up'>上一页</a><a href='show.php?page=$next'>下一页</a><a href='show.php?page=1'>尾页</a>";

?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>

	<center>
		<h1>展示页</h1>
		<table>
			<tr>
				<th>ID</th>
				<th>用户名</th>
				<th>密码</th>
				<th>手机号</th>
				<th>注册时间</th>
				<th>操作</th>

			</tr>
			<tbody id="tbody">
				<?php foreach ($data as $key => $value): ?>
				<tr>
					<td><?php echo $value['id'] ?></td>
					<td><?php echo $value['name'] ?></td>
					<td><?php echo $value['pwd'] ?></td>
					<td><?php echo $value['tel'] ?></td>
					<td><?php echo $value['add_date'] ?></td>
                    <td><a href="detail.php?id=<?php echo $value['id'] ?>">详情</a></td>
				</tr>
			    <?php endforeach ?>
			</tbody>
			
			
		</table>
		<!-- <?php echo $str; ?>    -->     
		<input type="button" class="button" value="首页">
		<input type="button" class="button" value="上一页">
		<input type="button" class="button" value="下一页">
		<input type="button" class="button" value="尾页">
		<input type="hidden" id="page" value="1">
		<input type="hidden" id="zpage" value="<?php echo $totalnum ?>">



	</center>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
	$(".button").click(function(){
		var obj = $(this).val();
		//alert(obj)
		if (obj == "首页") {
			page = 1;
		}
		if (obj == "上一页") {

			page = $("#page").val();
			page--;
		}
		if (obj == "下一页") {
			page = $("#page").val();
			page++;
		}
		if (obj == "尾页") {
			page = $("#zpage").val();
		}
		$.get("show.php",{page:page},function(res){
			if (res == false) {return false}
		    $("#page").val(page);
		    $("#tbody").html(res);
		})
	})
</script>
